<template>
    <div>
        <h2 align="center">登录页面</h2>
        <table class="table table-borderd">
            <tr>
                <td align="right">用户名</td>
                <td><input type="text" v-model="state.userName" /></td>
            </tr>
            <tr>
                <td align="right">密码</td>
                <td><input type="password" v-model="state.userPWd"  /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" value="登录" @click="login" class="btn btn-success" /></td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import {reactive,ref,toRefs} from 'vue'
import { useRouter,useRoute } from 'vue-router';
import {useUserStore} from '../store/User'
//创建store对象
const userStore=useUserStore();

import axios from 'axios';
const router=useRouter();
let data=reactive({
    state:{
        userName:'',
        userPWd:'' 
    }
})
let{state} =toRefs(data);

const login=()=>{
    
    if(state.value.userName==""||state.value.userPWd==""){
        alert("用户名或密码不能为空");
        return;
    }
    axios({
        url:"/api/Users/UsersLogin",
        method:'get',
        params:{
            username:state.value.userName,
            password:state.value.userPWd
        }
    })
    .then(res=>{
        if(res.data.data==null)
        {
            //用户不存在跳到注册页
            router.push("/regist");           
        }
        else{

            userStore.userInfo=res.data.data;
            
            axios({
            url:"/api/Users/SelById",
            method:'get',
            params:{
                uid:res.data.data.U_Id
            }
         })
         .then(d=>{
            if(d.data.data>0)
            {
                //用户已完善跳到主页面
                router.push("/main");
            }
            else{
                //用户未完善
                //跳到完善页面
                router.push({
                    path:'/userInfo',
                    query:{
                        id:res.data.data.U_Id,
                        name:state.value.userName
                    }
                });
            }
         })
        }
    })


}

</script>

<style>

</style>